package com.honey.generator.java;

import org.java.plugin.Plugin;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.configureation.ConfigurationPlugin;
import com.honey.configureation.holder.ProjectEnvironmentHolder;
import com.honey.core.Extension;
import com.honey.core.compiler.CompilationDescriptor;
import com.honey.core.compiler.CompilationType;
import com.honey.core.compiler.StorageType;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.generator.CodeCompilation;
import com.honey.core.utils.ObjectFactory;
import com.honey.generator.java.extension.point.GeneratorJavaClass;

public abstract class AbstractGenerator extends Extension<Plugin> implements CodeCompilation{
	
	private IntrospectSchema introspectSchema;
	
	private IntrospectSchemaColumn introspectSchemaColumn;
	
	public AbstractGenerator(Plugin belongPlugin) {
		super(belongPlugin);
	}

	
	@Override
	public CompilationDescriptor[] compilation(IntrospectSchema introspectSchema,IntrospectSchemaColumn introspectSchemaColumn) {
		this.introspectSchema = introspectSchema;
		this.introspectSchemaColumn = introspectSchemaColumn;
		
		CompilationDescriptor[] answer = null;
		try {
			ConfigurationPlugin configurationPlugin = (ConfigurationPlugin)(ObjectFactory.getRequestPlugin(this.getBelongPlugin(), "honey.configuration.plugin"));
			ProjectEnvironmentHolder projectEnvironment =  configurationPlugin.getContextInstant().getProjectEnvironmentHolder();
			
			String workespace = projectEnvironment.getTargetSrcDirectory() ;
			StorageType storageType = projectEnvironment.getStorageType();
			GeneratorJavaClass[] generatorJavaClasses = getGeneratorJavaClass() ;
			if(generatorJavaClasses != null && generatorJavaClasses.length >0 ){
				answer = new CompilationDescriptor[generatorJavaClasses.length];
				int i = 0;
				AbstractJavaCompilation abstractJavaCompilation =  null;
				for(GeneratorJavaClass generatorJavaClass : generatorJavaClasses ){
					abstractJavaCompilation = CompilationFactory.doJavaCompilation(getGeneratorJavaClassPlugin() , generatorJavaClass, introspectSchema, introspectSchemaColumn);
					if( abstractJavaCompilation != null ){
						answer[i] = new CompilationDescriptor(
							abstractJavaCompilation, 
							workespace, 
							CompilationType.JAVA, 
							storageType,
							generatorJavaClass.getContentType(), 
							projectEnvironment.getCharset()
						);
					}
					i++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return answer;
	}
	
	public IntrospectSchema getIntrospectSchema() {
		return introspectSchema;
	}
	
	public IntrospectSchemaColumn getIntrospectSchemaColumn() {
		return introspectSchemaColumn;
	}

	/**
	 * 获取生成java类
	 * @return
	 */
	public abstract GeneratorJavaClass[] getGeneratorJavaClass();
	
	
	/**
	 * 获取本插件
	 * @return
	 */
	public abstract GeneratorJavaClassPlugin getGeneratorJavaClassPlugin();
	
}
